﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using System.IO;

namespace SAD_SiS.Controllers
{
    public class AdvertisementController : Controller
    {
        //
        // GET: /Advertisement/

        /// <summary>
        /// Load advertisement slider
        /// </summary>
        /// <returns></returns>
        public ActionResult DemoAdvertisement()
        {
            using (SADEntities dbContext = new SADEntities())
            {
                var lstImg = (from advertise in dbContext.Advertisements where advertise.Active == true select advertise).ToList();
                return View(lstImg);
            }
        }

        /// <summary>
        /// Go to page edit image in silder
        /// </summary>
        /// <returns></returns>
        public ActionResult EditAdvertisement()
        {
            using (SADEntities dbContext = new SADEntities())
            {
                var lstImg = (from advertise in dbContext.Advertisements select advertise).ToList();
                return View(lstImg);
            }
        }


        /// <summary>
        /// Add new image to silder
        /// </summary>
        /// <returns></returns>
        public ActionResult InsertNewImg()
        {
            return View();
        }

        [HttpPost]
        public ActionResult InsertNewImg(HttpPostedFileBase file, FormCollection form)
        {
            string adLink = form["newAdlink"];
            Advertisement ad = new Advertisement();
            var imgName = Path.GetFileName(file.FileName);
            ad.ImgName = imgName;
            ad.Link = adLink;
            ad.Active = true;
            ad.count = 0;

            var path = Path.Combine(Server.MapPath("~/Content/sliderDemoImg/"), imgName);
            file.SaveAs(path);
            using (SADEntities dbContext = new SADEntities())
            {
                dbContext.Advertisements.Add(ad);
                dbContext.SaveChanges();
            }

            return RedirectToAction("EditAdvertisement");
        }

        /// <summary>
        /// Inactive an image in slider
        /// </summary>
        /// <param name="imgID"></param>
        /// <returns></returns>
        public ActionResult AdverInActive(string imgID)
        {
            int id = Convert.ToInt32(imgID);
            using (SADEntities dbContext = new SADEntities())
            {
                var ad = (from advertise in dbContext.Advertisements where advertise.AdImgID == id select advertise).Single();
                ad.Active = false;

                dbContext.SaveChanges();
            }
            return RedirectToAction("EditAdvertisement");
        }

        /// <summary>
        /// Active an image in slider
        /// </summary>
        /// <param name="imgID"></param>
        /// <returns></returns>
        public ActionResult AdverActive(string imgID)
        {
            int id = Convert.ToInt32(imgID);
            using (SADEntities dbContext = new SADEntities())
            {
                var ad = (from advertise in dbContext.Advertisements where advertise.AdImgID == id select advertise).Single();
                ad.Active = true;

                dbContext.SaveChanges();
            }
            return RedirectToAction("EditAdvertisement");
        }

        public ActionResult UpdateImgAd()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UpdateImgAd(HttpPostedFileBase fileupdate, FormCollection form)
        {
            string strLink = form["updateAdlink"];
            int id = Convert.ToInt32(form["imgidd"]);
            var imgAd = "";
            if (fileupdate!=null)
            {
                imgAd = Path.GetFileName(fileupdate.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/sliderDemoImg/"), imgAd);
                fileupdate.SaveAs(path);
            }
            using (SADEntities dbContext = new SADEntities())
            {
                var ad = (from advertise in dbContext.Advertisements where advertise.AdImgID == id select advertise).Single();
                if (fileupdate!=null && imgAd != "")
                {
                    ad.ImgName = imgAd;
                }
                if (strLink != null)
                {
                    ad.Link = strLink;
                }
                dbContext.SaveChanges();
            }

            return RedirectToAction("EditAdvertisement");
        }


        public ActionResult AdCount(string imgID)
        {
            int id = Convert.ToInt32(imgID);
            using (SADEntities dbContext = new SADEntities())
            {
                var ad = (from advertise in dbContext.Advertisements where advertise.AdImgID == id select advertise).Single();
                if (ad.count == null)
                {
                    ad.count = 0;
                }
                ad.count = ad.count + 1;

                dbContext.SaveChanges();
                return Redirect(ad.Link);
            }
        }

    }
}
